from rest_framework import serializers

from areas.models import Area
from users.models import Address


class AreasSerializer(serializers.ModelSerializer):
    class Meta:
        model = Area
        fields = '__all__'


class AddressesSerializer(serializers.ModelSerializer):
    province_id = serializers.IntegerField(write_only=True, min_value=100000)
    city_id = serializers.IntegerField(write_only=True, min_value=100000)
    district_id = serializers.IntegerField(write_only=True, min_value=100000)
    province = serializers.StringRelatedField(read_only=True)
    city = serializers.StringRelatedField(read_only=True)
    district = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = Address
        exclude = ('user',)

    def create(self, validated_data):
        user = self.context['request'].user
        validated_data['user'] = user
        return super(AddressesSerializer, self).create(validated_data)
